package com.qf.controller;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.config.WmsOrderReadListener;
import com.qf.entity.Order;
import com.qf.entity.WmsUser;
import com.qf.service.OrderService;
import com.qf.until.AliyunOSSUtil;
import com.qf.until.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;

/**
 * --- 天道酬勤 ---
 *
 * @author Xiaoze
 * @date 2024/7/23
 * @desc 订单管理
 */
@RestController
@RequestMapping("/wms/order")
@CrossOrigin
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 获取所有订单
     *
     * @param map
     * @return
     */
    @GetMapping("/list")
    public R findAll(@RequestParam HashMap<String, String> map) {
        int pageNum = Integer.parseInt(map.get("pageNum"));
        int PageSize = Integer.parseInt(map.get("pageSize"));
        PageHelper.startPage(pageNum, PageSize);
        List<Order> list = orderService.findAll(map);
        PageInfo<Order> info = new PageInfo<>(list);
        return R.ok(info);

    }

    /**
     * 添加订单
     */
    @PostMapping("/addOrder")
    public R addOrder(@RequestBody Order order) {
        orderService.addOrder(order);
        return R.ok();
    }

    /**
     * 上传图片
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public R upload( MultipartFile file) {
        String filePath = AliyunOSSUtil.upLoad(file);
        return R.ok(filePath);
    }

    /**
     * 根据id删除订单
     */
    @GetMapping("/delOrderById")
    public R delOrderById(int id) {
        boolean isOk = orderService.delOrderById(id);
        return R.ok(isOk);
    }

    /**
     * 批量删除
     */
    @GetMapping("/delBatch")
    public R delBatch(@RequestParam List<Integer> ids) {
        boolean isOk = orderService.delBatch(ids);
        return R.ok(isOk);
    }
    /**
     * 根据单行orderId 查询这一行的所有信息
     */
    @GetMapping("/detail")
    public R detail(int id) {
        List<Order> list = orderService.detail(id);
        return R.ok(list);
    }

    /**
     * 获取所有用户名
     * @return
     */
    @GetMapping("/findUserName")
    public R findUserName() {
        List<WmsUser> list = orderService.findUserName();
        return R.ok(list);
    }
    /**
     * 根据id更改确认收货人
     */
    @PostMapping("/updateAr")
    public R updateAr(@RequestBody Order order) {
        System.out.println("后端接收后得参数"+order);
        boolean isOk = orderService.updateAr(order);
        return R.ok(isOk);
    }
    /**
     * 上传Excel文件
     */
    @PostMapping("/upload/excel")
    public R uploadExcel(MultipartFile file) {
        try {
            // 重点,此处第三个参数需要传入Service对象
            EasyExcel.read(file.getInputStream( ),
                            Order.class,
                            new WmsOrderReadListener(orderService))
                    .sheet( ).doRead( );
        }catch (Exception e){
            e.printStackTrace();
            return R.fail( e.getMessage());
        }
        return R.ok( );
    }
    /**
     * 下载Excel文件
     * 注意,返回值是void
     */
    @GetMapping("/download/excel")
    public void downloadExcel(HttpServletResponse response) throws IOException {

        //  查询数据库全部数据
        List<Order> list = orderService.findAllList();

        // 下面这个注释是前端使用方式1,即a标签发请求时采用
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码
        String fileName = URLEncoder.encode("计量单位信息", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");

        // easyexcel导出工具
        EasyExcel.write(response.getOutputStream(), Order.class).autoCloseStream(Boolean.FALSE).sheet("计量单位信息")
                .doWrite(list);
    }
}
